# -*- coding:utf-8 -*-
from feature_data.XmlData2DictData import Xml2Dict
import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom

# reference https://www.cnblogs.com/jonyq/p/5929073.html
# reference https://www.cnblogs.com/linyawen/archive/2012/03/15/2398292.html

def sortedDictValues2(adict):
    keys = adict.keys()
    keys.sort()

    return [adict[key] for key in keys]

def sortedDictValues3(adict):
    keys = adict.keys()
    keys.sort()
    return map(adict.get, keys)

def sort_dict():
    adict = {1:'11', 2:'22'}
    print sortedDictValues2(adict)
    print sortedDictValues3(adict)
    print sortedDict(adict)

def sortedDict(adict):
    keys = adict.keys()
    keys.sort()
    newdict = {}
    for key in keys:
        newdict[key] = adict[key]
    return newdict

def dict_to_xml(input_dict_list, root_tag, node_tag):
    """ 定义根节点root_tag，定义第二层节点node_tag
    第三层中将字典中键值对对应参数名和值
       return: xml的tree结构 """
    root_name = ET.Element(root_tag)
    for input_dict in input_dict_list:
        input_dict = sortedDict(input_dict)
        keys = input_dict.keys()
        keys.sort()

        # for (k, v) in input_dict.items():
        for k in keys:
            node_name = ET.SubElement(root_name, node_tag)
            v = input_dict[k]
            # v => publication
            for key, val in v.items():
                key = ET.SubElement(node_name, key)
                key.text = val
    return root_name

def out_xml(root, out_file):
    """格式化root转换为xml文件"""
    rough_string = ET.tostring(root, 'utf-8')
    reared_content = minidom.parseString(rough_string)
    with open(out_file, 'w+') as fs:
        reared_content.writexml(fs, addindent=" ", newl="\n", encoding="utf-8")
    return True

def main1(paperid_list, publication_dict):

    list1 = [6, 32, 50]
    list2 = [0, 39, 47, 57]
    list3 = [10, 35, 46, 63, 75]
    list4 = [27, 37, 42, 53, 58, 65, 73, 82, 96, 98]
    list5 = [95]
    list_set = [list1, list2, list3, list4, list5]

    # paperid_ls = []
    # for ls in list_set:
    #     for idx in ls:
    #         paperid_ls.append(paperid_list[idx])

    publi_dict_list = []
    for ls in list_set:
        paperid_ls = []
        for idx in ls:
            paperid_ls.append(paperid_list[idx])
        publi_dict = {}
        for paperid in paperid_ls:
            publi_dict[paperid] = publication_dict[paperid]
        publi_dict_list.append(publi_dict)

    # [ {paperid: pulication} ]

    # publi_dict => paperid : publication
    out_file1 = "../analyze_data/cluster1.xml"

    root = dict_to_xml(publi_dict_list, "person", "publication")

    out_xml(root, out_file1)


def main2(paperid_list, publication_dict):

    list1 = [9, 14, 44, 85]
    list2 = [11]

    list_set = [list1, list2]


    publi_dict_list = []
    for ls in list_set:
        paperid_ls = []
        for idx in ls:
            paperid_ls.append(paperid_list[idx])
        publi_dict = {}
        for paperid in paperid_ls:
            publi_dict[paperid] = publication_dict[paperid]
        publi_dict_list.append(publi_dict)

    out_file2 = "../analyze_data/cluster2.xml"

    root = dict_to_xml(publi_dict_list, "person", "publication")

    out_xml(root, out_file2)


def main3(paperid_list, publication_dict):

    list1 = [90, 102]

    list_set = [list1]


    publi_dict_list = []
    for ls in list_set:
        paperid_ls = []
        for idx in ls:
            paperid_ls.append(paperid_list[idx])
        publi_dict = {}
        for paperid in paperid_ls:
            publi_dict[paperid] = publication_dict[paperid]
        publi_dict_list.append(publi_dict)

    out_file3 = "../analyze_data/cluster3.xml"

    root = dict_to_xml(publi_dict_list, "person", "publication")

    out_xml(root, out_file3)

def main4(paperid_list, publication_dict):


    list1 = [0, 6, 27, 32, 37, 39, 42, 47, 50, 53, 57, 58, 65, 73, 82, 95, 96, 98]
    list2 = [10, 35, 46, 63, 75]

    list3 = [1, 12, 16, 41, 91]
    list4 = [2, 45, 62, 70, 71, 81, 83, 113]
    list5 = [3, 109]
    list6 = [4, 28]
    list7 = [5, 21, 31, 121]
    list8 = [8, 99]
    list9 = [9, 14, 44, 85]
    list10 = [17, 84, 87, 108]
    list11 = [19, 22, 29, 34, 36, 54, 88, 94, 112, 119]
    list12 = [20, 76, 77, 79, 101, 104, 110, 116]
    list13 = [23, 61]
    list14 = [25, 59]
    list15 = [26, 74, 80, 103, 107]
    list16 = [30, 51]
    list17 = [49, 68, 72, 100, 105]
    list18 = [52, 90, 102]
    list19 = [60, 64, 86]
    list20 = [66, 67]
    list21 = [97, 118]
    list22 = [114, 115]

    list_set = [list1, list2,list3,list4,list5,list6,list7,list8,list9,list10,list11,list12,list13,list14,list15,list16,list17,
                list18,list19,list20,list21,list22,    [7],
    [11],
    [13],
    [15],
    [18],
    [24],
    [33],
    [38],
    [40],
    [43],
    [48],
    [55],
    [56],
    [69],
    [78],
    [89],
    [92],
    [93],
    [106],
    [111],
    [117],
    [120],
    [122],
    [123]]
    # list7 = [5, 21, 31, 121]
    # list_set =  [list7]

    i = 1
    for ls in list_set:
        paperid_ls = []
        for idx in ls:
            paperid_ls.append(paperid_list[idx])
        publi_org_dict = {}
        for paperid in paperid_ls:
            org = publication_dict[paperid]['organization']
            if org not in publi_org_dict:
                publi_org_dict[org] = 1
            else:
                publi_org_dict[org] += 1
        print publi_org_dict
        # print i, sorted(publi_org_dict.keys(), key=lambda d: d[1], reverse=True)
        i += 1


def main_out_file():
    xml_filepath = "../raw_data_file/Bo Liu.xml"

    dataset = Xml2Dict(xml_filepath)

    paperid_list = dataset.paperid_list

    publication_dict = dataset.publication_dict

    main4(paperid_list, publication_dict)




if __name__ == '__main__':
    # sort_dict()

    main_out_file()


